import { contextBridge ,ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
    contextBridge.exposeInMainWorld('db', {
      select: (sql,...params) => ipcRenderer.invoke('db:select',sql,...params),
      update: (sql,...params) => ipcRenderer.invoke('db:update',sql,...params)
    })
    contextBridge.exposeInMainWorld('operation', {
      maximize: () => ipcRenderer.invoke('operation:maximize'),
      restore: () => ipcRenderer.invoke('operation:restore'),
      openFile: () => ipcRenderer.invoke('operation:openFile'),
      openFADir: () => ipcRenderer.invoke('operation:openFADir'),
      readFileContent: (path) => ipcRenderer.invoke('operation:readFileContent',path),
      getPDF: (data) => ipcRenderer.invoke('operation:getPDF',data),
      openDirectoryInExplorer: () => ipcRenderer.invoke('operation:openDirectoryInExplorer'),
    })
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
